package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.health.dao.MemberDao;
import com.itheima.health.dao.OrderDao;
import com.itheima.health.dao.ReportDao;
import com.itheima.health.service.ReportService;
import com.itheima.health.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author likepei
 * @Date 2020/2/21 10:52
 * @Version v1.0
 * @Description 图表的业务实现类
 */
@Service
@Slf4j
@Transactional
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportDao reportDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;

    /**
     * 获取指定月份列表的 会员数量
     * @param monthList
     * @return
     */
    @Override
    public List<Integer> findMemberCountByMonth(List<String> monthList) {
        List<Integer> memberCount = new ArrayList<>();

        //遍历获取到 每一个月份
        for (String month : monthList) {
            // month ===>  2020.04
            month = month+".31"; // 拼接后的日期 格式            2020.04.31

            //调用dao, 得到指定月份的会员累计数量
            Integer count = reportDao.findMemberCountBeforeDate( month );
            memberCount.add(count);
        }

        return memberCount;
    }

    /**
     * 套餐占比数据
     * @return
     */
    @Override
    public List<Map<String, Object>> findSetmealCount() {
        return reportDao.findSetmealCount();
    }

    /**
     * 获取运营统计数据
     * Map数据格式：
     *	    reportDate -> String 				// 报表日期
     *      todayNewMember -> Integer			// 今日新增会员
     *      thisWeekNewMember -> Integer		// 本周新增会员
     *      thisMonthNewMember -> Integer		// 本月新增会员
     *      totalMember -> Integer		    	// 总会员
     *      todayOrderNumber -> Integer			// 今日预约人数
     *      thisWeekOrderNumber -> Integer		// 本周预约人数
     *      thisMonthOrderNumber -> Integer		// 本月预约人数
     *      todayVisitsNumber -> Integer		// 今日到诊人数
     *      thisWeekVisitsNumber -> Integer		// 本周到诊人数
     *      thisMonthVisitsNumber -> Integer	// 本月到诊人数
     *      hotSetmeal -> List<Map<String,Object>>	// 热门套餐
     * @return
     */
    @Override
    public Map<String, Object> getBusinessReportData() {
        try {
            //获得今天日期
            //String today = "2020-02-22";
            String today = DateUtils.parseDate2String(DateUtils.getToday());
            //获得本周第一天日期(周一)
            //String thisWeekMonday = "2020-02-17";
            String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());

            //获得本月第一天日期
            //String thisMonthFirst = "2020-02-01";
            String thisMonthFirst = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());

            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>today = " + today);
            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>thisWeekMonday = " + thisWeekMonday);
            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>thisMonthFirst = " + thisMonthFirst);

            //获取与会员有关数据 (今日、本周、本月、总人数)
            Integer todayNewMember = memberDao.findMemberCountByDate(today);
            Integer thisWeekNewMember = memberDao.findMemberCountAfterDate(thisWeekMonday);
            Integer thisMonthNewMember = memberDao.findMemberCountAfterDate(thisMonthFirst);
            Integer totalMember = memberDao.findMemberTotalCount();

            //获取与预约有关数据 (今日、本周、本月)
            Integer todayOrderNumber = orderDao.findOrderCountByDate(today);
            Integer thisWeekOrderNumber = orderDao.findOrderCountByAfterDate(thisWeekMonday);
            Integer thisMonthOrderNumber = orderDao.findOrderCountByAfterDate(thisMonthFirst);

            //获得到诊相关数据 (今日、本周、本月)
            Integer todayVisitsNumber = orderDao.findVisitsCountByDate(today);
            Integer thisWeekVisitsNumber = orderDao.findVisitsCountByAfterDate(thisWeekMonday);
            Integer thisMonthVisitsNumber = orderDao.findVisitsCountByAfterDate(thisMonthFirst);

            //获得套餐热门数据
            List<Map> hotSetmeal = orderDao.findHotSetmeal();

            //封装数据到map集合
            Map<String, Object> resultMap = new HashMap<>();
            //封装报表日期
            resultMap.put("reportDate", today);
            //会员有关数据 (今日、本周、本月、总人数)
            resultMap.put("todayNewMember", todayNewMember);
            resultMap.put("thisWeekNewMember", thisWeekNewMember);
            resultMap.put("thisMonthNewMember", thisMonthNewMember);
            resultMap.put("totalMember", totalMember);
            //获取与预约有关数据 (今日、本周、本月)
            resultMap.put("todayOrderNumber", todayOrderNumber);
            resultMap.put("thisWeekOrderNumber", thisWeekOrderNumber);
            resultMap.put("thisMonthOrderNumber", thisMonthOrderNumber);
            //到诊相关数据 (今日、本周、本月)
            resultMap.put("todayVisitsNumber", todayVisitsNumber);
            resultMap.put("thisWeekVisitsNumber", thisWeekVisitsNumber);
            resultMap.put("thisMonthVisitsNumber", thisMonthVisitsNumber);
            //套餐热门数据
            resultMap.put("hotSetmeal", hotSetmeal);

            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }
}
